<?php
error_reporting(0);

$mytrustdirpath = dirname( __FILE__ ) ;

require_once $mytrustdirpath.'/include/make_rss.inc.php';

$LOGO = 'images/logo.gif';
$module_name = htmlspecialchars( $xoopsModule->getVar('name'), ENT_QUOTES );
$cid = ( ! empty($_GET['cid'] ) )? intval( $_GET['cid'] ) : 0;
$e = ( !empty( $_GET['e'] ) )? $_GET['e'] : '';

if ($e === 'sjis') {
	$encode = 'SJIS';
	$encoding = 'Shift-JIS';
} else {
	$encode = $encoding = 'UTF-8';
}

$module_handler =& xoops_gethandler('module');
$config_handler =& xoops_gethandler('config');
$module =& $module_handler->getByDirname($mydirname);
$config =& $config_handler->getConfigsByCat(0, $module->getVar('mid') );

if ( $config['per_rss'] )  $rss_limit = $config['per_rss'];
if (! $rss_limit) $rss_limit = '15';
$c_file = XOOPS_ROOT_PATH.'/cache/'.$mydirname.'_'.$cid.'.rss';
if ( file_exists( $c_file ) && filemtime( $c_file )+300 > time() ){
	$data = unserialize(join('',file( $c_file ) ) );
	$b_time = filemtime( $c_file );
} else {
	$data = d3downloads_rss_new( $mydirname, $rss_limit, 0, $cid );
	if ($fp = @fopen($c_file,"wb"))
	{
		fputs( $fp,serialize( $data ) );
		fclose($fp);
	}
	$b_time = time();
}

$title = ( $cid )? htmlspecialchars( $data[0]['cat_name'],ENT_QUOTES). ' - ' . htmlspecialchars($xoopsConfig['sitename'],ENT_QUOTES): $module_name. ' - ' . htmlspecialchars( $xoopsConfig['sitename'],ENT_QUOTES );
$top_link = ( $cid )? "index.php?cid=".$cid : "";

$dimention = getimagesize( XOOPS_ROOT_PATH.'/'.$LOGO );
if (empty($dimention[0])) {
	$width = 88;
} else {
	$width = ($dimention[0] > 144) ? 144 : $dimention[0];
}
if (empty($dimention[1])) {
		$height = 31;
} else {
	$height = ($dimention[1] > 400) ? 400 : $dimention[1];
}

$image_url = XOOPS_URL.'/'.$LOGO;
$image_title = $title;
$image_link = XOOPS_URL.'/modules/'.$mydirname.'/'.$top_link;
$image_width = $width;
$image_height = $height;

// RSS Build
$out = '<?xml version="1.0" encoding="'.$encoding.'"?>
<?xml-stylesheet type="text/xsl" media="screen" href="'.XOOPS_URL.'/modules/'.$mydirname.'/rss2html.xsl" ?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>'.$title.'</title>
  <link>'.XOOPS_URL.'/modules/'.$mydirname.'/'.$top_link.'</link>
  <description></description>
  <lastBuildDate>'.date('r', $b_time).'</lastBuildDate>
  <docs>http://backend.userland.com/rss/</docs>
  <image>
    <title>'.$image_title.'</title>
    <link>'.$image_link.'</link>
    <url>'.$image_url.'</url>
    <width>'.$image_width.'</width>
    <height>'.$image_height.'</height>
  </image>
';
foreach($data as $item) {
	$out .='  <item>
    <title>'.htmlspecialchars( d3downloads_make_context( strip_tags( $item['title'] ) ), ENT_QUOTES).'</title>
    <link>'.$item['link'].'</link>
    <guid>'.$item['link'].'</guid>
    <description>'.htmlspecialchars( d3downloads_make_context( strip_tags( $item['description'] ) ),ENT_QUOTES ).'</description>
    <pubDate>'.date("r", $item['time']).'</pubDate>
    <category>'.$item['cat_name'].'</category>
    <content:encoded><![CDATA['.$item['description'].']]></content:encoded>
  </item>
';
}
$out .='</channel>
</rss>';

$out = str_replace("\0", '', mb_convert_encoding($out, $encode, _CHARSET));

header('Content-type: application/xml');
echo $out;

?>